/*
 * Busqueda.java
 *
 * Created on June 30, 2007, 4:24 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package Modelo;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.Vector;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 *
 * @author tuza
 */
public class Busqueda {
 
    private Vector titulos;
    private Vector paths;
    private Vector ids;
    
    
    /** Creates a new instance of Busqueda */
    //hace una busqueda unitaria
    public Busqueda( Vector titulos, Vector paths, Vector ids) {
        this.setTitulos(titulos); 
        this.setPaths(paths);
        this.setIds(ids);
    }
    
   
    public static Busqueda fromXml( String str )
    {
        InputStream is = new ByteArrayInputStream( str.getBytes() );
        
        // InputStream ise = new InputStream();
        
        try
        {
            DocumentBuilderFactory fact = DocumentBuilderFactory.newInstance();
            DocumentBuilder parser = fact.newDocumentBuilder();
            Document doc = parser.parse( is );
            
            return fromXml( doc );
        }
        catch( Exception ex )
        {
            ex.printStackTrace();
            return null;
        }
    }
        
    public static Busqueda fromXml( Document doc )
    {
        
        Vector titulos = new Vector(); 
        Vector paths = new Vector();
        Vector ids = new Vector();
      
        try
        {
            NodeList title = doc.getElementsByTagName( "title" );
            NodeList path = doc.getElementsByTagName("path");
            NodeList id = doc.getElementsByTagName("id");
            
            for( int i = 0 ; i < title.getLength() ; i++ )
            {
                Node t = title.item(i).getFirstChild();
                titulos.addElement(t.getNodeValue());
                
                Node p = path.item(i).getFirstChild();
                paths.addElement(p.getNodeValue());
                
                Node d = id.item(i).getFirstChild();
                ids.addElement(d.getNodeValue());
            }
            
            return new Busqueda( titulos, paths, ids );
        }
        catch( Exception ex )
        {
            ex.printStackTrace();
            return null;
        }
  }

    public Vector getTitulos() {
        return titulos;
    }

    public void setTitulos(Vector titulos) {
        this.titulos = titulos;
    }

    public Vector getPaths() {
        return paths;
    }

    public void setPaths(Vector paths) {
        this.paths = paths;
    }

    public Vector getIds() {
        return ids;
    }

    public void setIds(Vector ids) {
        this.ids = ids;
    }
  
    
    
    
    
    
    
}
